package com.csmz.mybookreader.UI;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.csmz.mybookreader.R;
import com.csmz.mybookreader.View.DrawerView;
import com.csmz.mybookreader.View.MyInfoView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * 中间内容栏
     * @param savedInstanceState
     */
    private FrameLayout mBodyLayout;

    /**
     * 全部内容栏
     * @param savedInstanceState
     */
    private ConstraintLayout mBodyConLayout;

    /**
     * 底部按钮栏
     * @param savedInstanceState
     */
    private LinearLayout mBottomLayout;

    /**
     * 底部按钮
     * @param savedInstanceState
     */
    private View mCourseBtn;
    private View mExercisesBtn;
    private View mMyInfoBtn;
    private TextView tv_course,tv_exercises,tv_myInfo;
    private ImageView iv_course,iv_exercises,iv_myInfo;
    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout rl_title_bar,rl_main_rl1,rl_main_rl2;
    private MyInfoView mMyInfoView;
    private DrawerView mDrawerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        initBottomBar();
        setListener();
        setInitStatus();
    }
    /**
     * 获取界面上的ui控件
     */
    private void init() {
        rl_main_rl1=findViewById(R.id.main_relative1);
        rl_main_rl2=findViewById(R.id.main_relative2);
        tv_back= (TextView) findViewById(R.id.tv_back);
        tv_main_title= (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("目录");
        rl_title_bar= (RelativeLayout) findViewById(R.id.rl_title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        tv_back.setVisibility(View.GONE);
        initBodyLayout();
    }
    /**
     * 获取底部导航栏上的控件
     */
    private void initBottomBar(){
        mBottomLayout= findViewById(R.id.main_bottom_bar);
        mCourseBtn=findViewById(R.id.bottom_bar_course);
        mExercisesBtn=findViewById(R.id.bottom_bar_exercises);
        mMyInfoBtn=findViewById(R.id.bottom_bar_myinfo);
        tv_course=  findViewById(R.id.bottom_bar_text_course);
        tv_exercises=  findViewById(R.id.bottom_bar_text_exercises);
        tv_course= findViewById(R.id.bottom_bar_text_course);
        tv_myInfo=  findViewById(R.id.bottom_bar_text_myinfo);
        iv_course=  findViewById(R.id.bottom_bar_image_course);
        iv_exercises=  findViewById(R.id.bottom_bar_image_exercises);
        iv_myInfo=  findViewById(R.id.bottom_bar_image_myinfo);
    }

    private void initBodyLayout() {
        //mBodyConLayout=findViewById(R.id.drawer_layout);
        mBodyLayout = findViewById(R.id.main_body);
    }

    /**
     * 设置界面View的初始化状态
     */
    private void setInitStatus() {
        clearBottomImageState();
        setSelectedStatus(0);
        createView(0);
    }

    /**
     * 显示对应的页面
     */
    private void selectDisplayView(int index){
        removeAllView();
        createView(index);
        setSelectedStatus(index);
    }

    /**
     *
     * 选择视图
     * @param viewIndex
     */
    private void createView(int viewIndex) {
        switch (viewIndex){
            case 0:
                //书库界面
                if (mDrawerView ==null){
                    mDrawerView=new DrawerView(this);
                    mBodyLayout.addView(mDrawerView.getView());
                    //mBodyConLayout.addView(mDrawerView.getView());
                }else {
                    mDrawerView.getView();
                }
                mDrawerView.showView();
                break;
            case 1:
                //目录界面
                break;
            case 2:
                //我的界面
                if (mMyInfoView == null){
                    mMyInfoView=new MyInfoView(this);
                    mBodyLayout.addView(mMyInfoView.getView());
                }else {
                    mMyInfoView.getView();
                }
                mMyInfoView.showView();

                break;
        }
    }

    /**
     * 设置底部三个按钮的点击监听事件
     */
    private void setListener() {
        for (int i=0;i<mBottomLayout.getChildCount();i++){
            mBottomLayout.getChildAt(i).setOnClickListener(this);
        }
    }


    /**
     * 控件的点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //书库点击事件
            case R.id.bottom_bar_course:
                clearBottomImageState();
                selectDisplayView(0);
                break;
            //目录点击事件
            case R.id.bottom_bar_exercises:
                clearBottomImageState();
                selectDisplayView(1);
                break;
            //我的点击事件
            case R.id.bottom_bar_myinfo:
                clearBottomImageState();
                selectDisplayView(2);
                break;
            default:
                break;
        }
    }

    /**
     * 清除底部按钮的选中状态
     *
     */
    private void clearBottomImageState() {
        tv_course.setTextColor(Color.parseColor("#666666"));
        tv_exercises.setTextColor(Color.parseColor("#666666"));
        tv_myInfo.setTextColor(Color.parseColor("#666666"));
        iv_course.setImageResource(R.drawable.main_course_icon_bw);
        iv_exercises.setImageResource(R.drawable.main_exercises_icon_bw);
        iv_myInfo.setImageResource(R.drawable.main_my_icon_bw);
        for (int i=0;i<mBottomLayout.getChildCount();i++){          //getChildCount()  getChildAt()
            mBottomLayout.getChildAt(i).setSelected(false);
        }
    }

    /**
     * 设置底部按钮的选中状态
     *
     */
    private void setSelectedStatus(int index){
        switch (index){
            case 0:
                mCourseBtn.setSelected(true);
                iv_course.setImageResource(R.drawable.main_course_icon);
                tv_course.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("书架");
                break;
            case 1:
                mExercisesBtn.setSelected(true);
                iv_exercises.setImageResource(R.drawable.main_exercises_icon);
                tv_exercises.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("目录");
                break;
            case 2:
                mMyInfoBtn.setSelected(true);
                iv_myInfo.setImageResource(R.drawable.main_my_icon);
                tv_myInfo.setTextColor(Color.parseColor("#0097F7"));
                tv_main_title.setText("我");
                tv_myInfo.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 移除不需要的视图
     */
    private void removeAllView(){
        for (int i =0;i<mBodyLayout.getChildCount();i++){
            mBodyLayout.getChildAt(i).setVisibility(View.GONE);
        }
        /*for (int j=0;j<mBodyConLayout.getChildCount();j++){
            mBodyConLayout.getChildAt(j).setVisibility(View.GONE);
        }*/
    }

    protected long exitTime;//记录第一次点击时的时间
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if ((System.currentTimeMillis() - exitTime) > 2000){
                Toast.makeText(MainActivity.this,"再按一次退出博学谷", Toast.LENGTH_SHORT).show();
                exitTime= System.currentTimeMillis();
            }else {
                MainActivity.this.finish();
                if (readLoginStatus()){
                    clearLoginStatus();
                }
                System.exit(0);
            }
            return true;
        }
        return  super.onKeyDown(keyCode,event);
    }

    /**
     * 清除SharedPreferences中的登录状态
     */
    private void clearLoginStatus() {
        SharedPreferences sp=getSharedPreferences("LoginInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();   //获取编辑器
        editor.putBoolean("isLogin",false);    //清除登录状态
        editor.putString("loginUserName","");   //清除登录时用户名
        editor.commit();    //提交修改
    }


    /**
     * 获取SharedPreferences中的登录状态
     * @return
     */
    private boolean readLoginStatus() {
        SharedPreferences sp = getSharedPreferences("LoginInfo", Context.MODE_PRIVATE);
        boolean isLogin=sp.getBoolean("isLogin",false);
        return isLogin;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (data!=null){
            //从设置界面或登录界面传递过来的登录状态
            boolean isLogin=data.getBooleanExtra("isLogin",false);
            if (isLogin){
                clearBottomImageState();
                selectDisplayView(0);
            }
            if (mMyInfoView != null){
                //登录成功或退出登录时根据isLogin设置我的界面
                mMyInfoView.setLoginParams(isLogin);
            }

        }

    }

}
